import json

from config import *


def Get_DescribeDomainRecords(accessKeyId, accessSecret, city, query_RR,query_DomainName):
    # 获取根据传入参数获取指定主域名的所有解析记录列表
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException
    from aliyunsdkcore.acs_exception.exceptions import ServerException
    from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest

    client = AcsClient(accessKeyId, accessSecret, city)

    request = DescribeDomainRecordsRequest()
    request.set_accept_format('json')
    request.set_DomainName(query_DomainName)
    response = client.do_action_with_exception(request)

    jsonDomainRecords = json.loads(str(response, encoding='utf-8'))

    count = 0

    for i in jsonDomainRecords["DomainRecords"]["Record"]:
        count += 1
        if i["RR"] == query_RR:
            return (i["RecordId"], i["Value"])
        else:
            if count == len(jsonDomainRecords["DomainRecords"]["Record"]):
                print("找不到" + query_RR + "请前往控制台检查是否已创建该子域名（RR）")
                return None, None


# 统一入口
if __name__ == '__main__':
    Get_DescribeDomainRecords(accessKeyId,
                                    accessSecret,
                                    city,
                                    query_RR,
                                    query_DomainName)
